home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks97 / WarriorsProgress.sit / Warrior’s Progress / source code / Source / Libraries / Files / FileReader.cp < prev    next >
Text File  |  1997-06-28  |  1KB  |  64 lines

  1. // FileReader.cp
  2.  
  3. #ifndef FileReader_h
  4. #include "FileReader.h"
  5. #endif
  6. #ifndef Buffer_h
  7. #include "Buffer.h"
  8. #endif
  9. #ifndef FileAccessPath_h
  10. #include "FileAccessPath.h"
  11. #endif
  12. #ifndef FilePermissionError_h
  13. #include "FilePermissionError.h"
  14. #endif
  15. #ifndef FileLockError_h
  16. #include "FileLockError.h"
  17. #endif
  18.  
  19. FileReader::FileReader( const FileAccessPath& theFile,
  20.                                 uint32 position )
  21.   : finished( false )
  22.   {
  23.     Assert( theFile.IsOpen() );
  24.     
  25.     ioParam.ioCompletion = 0;
  26.     ioParam.ioRefNum = theFile.RefNum();
  27.     ioParam.ioPosMode = fsFromStart;
  28.     ioParam.ioPosOffset = position;
  29.   }
  30.  
  31. uint32 FileReader::operator>>( Buffer& buffer )
  32.   {
  33.     Assert( !Finished() );
  34.     
  35.     ioParam.ioBuffer = reinterpret_cast<char *>( buffer.Unused().Start() );
  36.     ioParam.ioReqCount = buffer.Unused().Length();
  37.     
  38.     OSErr error = PBReadSync( this );
  39.     buffer.AdvanceMark( ioParam.ioActCount );
  40.     
  41.     if ( error == eofErr )
  42.         finished = true;
  43.      else
  44.         ThrowError( error );
  45.     
  46.     return ioParam.ioActCount;
  47.   }
  48.  
  49. void FileReader::ThrowError( OSErr error )
  50.   {
  51.     if ( error == noErr )
  52.         return;
  53.     
  54.     switch ( error )
  55.       {
  56.         case afpAccessDenied:    throw FilePermissionError( error );
  57.         
  58.             // This is a documented possibility:  (?)
  59.         case fLckdErr:                throw FileLockError( error );
  60.       }
  61.     
  62.     throw FileError( error );
  63.   }
  64.